package sap.gui;

import data.*;
import exceptions.*;
import sap.provider.*;

import javax.swing.*;
/*
 * MilestonManager.java
 *
 * Created on 12 de noviembre de 2007, 06:06 PM
 */

/**
 *
 * @author  Enrique Garc�a
 */
public class MilestonManager extends javax.swing.JPanel {
    
    /** Creates new form MilestonManager */
    public MilestonManager(CManager cm) {
        initComponents();
        this.cm=cm;
        init();

    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    

    public void init()
    {
        jPanel1.setVisible(false);
        run.setVisible(false);
        jTextField1.setEditable(false);
        jTextField1.setText(cm.psp);
        jTextField2.setEnabled(true);
        jTextField2.setEditable(true);
    }
    
    public void initC()
    {
        jPanel1.setVisible(false);
        run.setVisible(false);
        jTextField1.setEditable(false);
        jTextField1.setText(cm.psp);
        jTextField2.setEnabled(true);
        jTextField2.setEditable(true);
        this.Tipos.setSelectedIndex(0);
    }



    public void clear()
    {
        jTextField2.setText("");
        jTextField3.setText("");
		jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
		jTextField7.setText("");
        jTextField8.setText("");
        jTextField9.setText("");
		jTextField10.setText("");
        jTextField11.setText("");
        jTextField12.setText("");
        jTextField13.setText("");
        jTextField14.setText("");
        jTextField15.setText("");
		jTextField16.setText("");
        jTextField17.setText("");
        jTextField18.setText("");
		jTextField19.setText("");
        jTextField20.setText("");
        jTextField21.setText("");
        jTextField22.setText("");
		jTextField23.setText("");
        jTextField24.setText("");
		jTextField25.setText("");
    }

    private void setDialogFromStructure(Milestone milestone) {
        jTextField3.setText(milestone.milestone_usage);
		jTextField4.setText(milestone.description);
        jTextField5.setText(milestone.sched_milestone_date_basic);
        jTextField6.setText(milestone.sched_milestone_time_basic);
		jTextField7.setText(milestone.sched_milestone_date_forecast);
        jTextField8.setText(milestone.sched_milestone_time_forecast);
        jTextField9.setText(milestone.fixed_milestone_date_basic);
		jTextField10.setText(milestone.fixed_milestone_time_basic);
        jTextField11.setText(milestone.fixed_milestone_date_forecast);
        jTextField12.setText(milestone.fixed_milestone_time_forecast);
        jTextField13.setText(milestone.actual_date);
        jTextField14.setText(milestone.actual_time);
        jTextField15.setText(milestone.latest_dates_indicator);
		jTextField16.setText(milestone.offset_start_end_indicator);
        jTextField17.setText(milestone.offset_milestone_date);
        jTextField18.setText(milestone.offset_milestone_date_unit);
		jTextField19.setText(milestone.offset_milestone_date_unit_iso);
        jTextField20.setText(milestone.offset_percentage);
        jTextField21.setText(milestone.trend_analysis_indicator);
        jTextField22.setText(milestone.earned_value_indicator);
        jTextField23.setText(milestone.percentage_of_completion);
        jTextField24.setText(milestone.sales_doc_date_inidicator);
        jTextField25.setText(milestone.invoice_percentage);

	}


    private Milestone getListFromDialog() {

        Milestone milestone = new Milestone();
        milestone.wbs_element = jTextField1.getText();
        milestone.milestone_number = jTextField2.getText();
		milestone.milestone_usage = jTextField3.getText();
        milestone.description = jTextField4.getText();
        milestone.sched_milestone_date_basic = jTextField5.getText();
        milestone.sched_milestone_time_basic = jTextField6.getText();
        milestone.sched_milestone_date_forecast = jTextField7.getText();
        milestone.sched_milestone_time_forecast = jTextField8.getText();
        milestone.fixed_milestone_date_basic = jTextField9.getText();
        milestone.fixed_milestone_time_basic = jTextField10.getText();
        milestone.fixed_milestone_date_forecast = jTextField11.getText();
        milestone.fixed_milestone_time_forecast = jTextField12.getText();
        milestone.actual_date = jTextField3.getText();
        milestone.actual_time = jTextField4.getText();
        milestone.latest_dates_indicator = jTextField15.getText();
        milestone.offset_start_end_indicator = jTextField16.getText();
        milestone.offset_milestone_date = jTextField17.getText();
        milestone.offset_milestone_date_unit = jTextField18.getText();
        milestone.offset_milestone_date_unit_iso = jTextField19.getText();
        milestone.offset_percentage= jTextField20.getText();
        milestone.trend_analysis_indicator = jTextField21.getText();
        milestone.earned_value_indicator = jTextField22.getText();
        milestone.percentage_of_completion = jTextField23.getText();
        milestone.sales_doc_date_inidicator = jTextField24.getText();
        milestone.invoice_percentage = jTextField25.getText();



                return milestone;

    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        run = new javax.swing.JButton();
        Tipos = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jTextField10 = new javax.swing.JTextField();
        jTextField11 = new javax.swing.JTextField();
        jTextField12 = new javax.swing.JTextField();
        jTextField13 = new javax.swing.JTextField();
        jTextField14 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jTextField15 = new javax.swing.JTextField();
        jTextField16 = new javax.swing.JTextField();
        jTextField17 = new javax.swing.JTextField();
        jTextField18 = new javax.swing.JTextField();
        jTextField19 = new javax.swing.JTextField();
        jTextField20 = new javax.swing.JTextField();
        jTextField22 = new javax.swing.JTextField();
        jTextField21 = new javax.swing.JTextField();
        jTextField23 = new javax.swing.JTextField();
        jTextField24 = new javax.swing.JTextField();
        jTextField25 = new javax.swing.JTextField();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();

        setBackground(new java.awt.Color(0, 0, 0));
        setLayout(null);

        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Choose one function");
        add(jLabel1);
        jLabel1.setBounds(10, 10, 100, 20);

        run.setBackground(new java.awt.Color(0, 0, 0));
        run.setText("Run");
        run.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runActionPerformed(evt);
            }
        });
        add(run);
        run.setBounds(620, 20, 130, 30);

        Tipos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one function", "Create Mileston", "Edit Milestone", "Delete Milestone" }));
        Tipos.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                TiposItemStateChanged(evt);
            }
        });
        add(Tipos);
        Tipos.setBounds(10, 30, 180, 20);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Milestone Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(null);

        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("MILESTONE_USAGE");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(30, 40, 210, 14);

        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("DESCRIPTION");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(30, 70, 210, 14);

        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("SCHED_MILESTONE_DATE_BASIC");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(30, 100, 210, 14);

        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("SCHED_MILESTONE_TIME_BASIC");
        jPanel1.add(jLabel8);
        jLabel8.setBounds(30, 130, 210, 14);

        jLabel9.setForeground(new java.awt.Color(255, 255, 255));
        jLabel9.setText("SCHED_MILESTONE_DATE_FORECAST");
        jPanel1.add(jLabel9);
        jLabel9.setBounds(30, 160, 210, 14);

        jLabel10.setForeground(new java.awt.Color(255, 255, 255));
        jLabel10.setText("SCHED_MILESTONE_TIME_FORECAST");
        jPanel1.add(jLabel10);
        jLabel10.setBounds(30, 190, 210, 14);

        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("FIXED_MILESTONE_DATE_BASIC");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(30, 220, 210, 14);

        jLabel12.setForeground(new java.awt.Color(255, 255, 255));
        jLabel12.setText("FIXED_MILESTONE_TIME_BASIC");
        jPanel1.add(jLabel12);
        jLabel12.setBounds(30, 250, 210, 14);

        jLabel13.setForeground(new java.awt.Color(255, 255, 255));
        jLabel13.setText("FIXED_MILESTONE_DATE_FORECAST");
        jPanel1.add(jLabel13);
        jLabel13.setBounds(30, 280, 210, 14);

        jLabel14.setForeground(new java.awt.Color(255, 255, 255));
        jLabel14.setText("FIXED_MILESTONE_TIME_FORECAST");
        jPanel1.add(jLabel14);
        jLabel14.setBounds(30, 310, 210, 14);

        jLabel15.setForeground(new java.awt.Color(255, 255, 255));
        jLabel15.setText("ACTUAL_DATE");
        jPanel1.add(jLabel15);
        jLabel15.setBounds(30, 340, 210, 14);

        jLabel16.setForeground(new java.awt.Color(255, 255, 255));
        jLabel16.setText("ACTUAL_TIME");
        jPanel1.add(jLabel16);
        jLabel16.setBounds(30, 370, 210, 14);
        jPanel1.add(jTextField3);
        jTextField3.setBounds(230, 40, 100, 20);
        jPanel1.add(jTextField4);
        jTextField4.setBounds(230, 70, 100, 20);
        jPanel1.add(jTextField5);
        jTextField5.setBounds(230, 100, 100, 20);
        jPanel1.add(jTextField6);
        jTextField6.setBounds(230, 130, 100, 20);
        jPanel1.add(jTextField7);
        jTextField7.setBounds(230, 160, 100, 20);
        jPanel1.add(jTextField8);
        jTextField8.setBounds(230, 190, 100, 20);
        jPanel1.add(jTextField9);
        jTextField9.setBounds(230, 220, 100, 20);
        jPanel1.add(jTextField10);
        jTextField10.setBounds(230, 250, 100, 20);
        jPanel1.add(jTextField11);
        jTextField11.setBounds(230, 280, 100, 20);
        jPanel1.add(jTextField12);
        jTextField12.setBounds(230, 310, 100, 20);
        jPanel1.add(jTextField13);
        jTextField13.setBounds(230, 340, 100, 20);
        jPanel1.add(jTextField14);
        jTextField14.setBounds(230, 370, 100, 20);

        jLabel17.setForeground(new java.awt.Color(255, 255, 255));
        jLabel17.setText("LATEST_DATES_INDICATOR");
        jPanel1.add(jLabel17);
        jLabel17.setBounds(440, 40, 200, 14);

        jLabel18.setForeground(new java.awt.Color(255, 255, 255));
        jLabel18.setText("OFFSET_START_END_INDICATOR");
        jPanel1.add(jLabel18);
        jLabel18.setBounds(440, 70, 200, 14);

        jLabel19.setForeground(new java.awt.Color(255, 255, 255));
        jLabel19.setText("OFFSET_MILESTONE_DATE");
        jPanel1.add(jLabel19);
        jLabel19.setBounds(440, 100, 200, 14);

        jLabel20.setForeground(new java.awt.Color(255, 255, 255));
        jLabel20.setText("OFFSET_MILESTONE_DATE_UNIT");
        jPanel1.add(jLabel20);
        jLabel20.setBounds(440, 130, 200, 14);

        jLabel21.setForeground(new java.awt.Color(255, 255, 255));
        jLabel21.setText("OFFSET_MILESTONE_DATE_UNIT_ISO");
        jPanel1.add(jLabel21);
        jLabel21.setBounds(440, 160, 200, 14);

        jLabel22.setForeground(new java.awt.Color(255, 255, 255));
        jLabel22.setText("OFFSET_PERCENTAGE");
        jPanel1.add(jLabel22);
        jLabel22.setBounds(440, 190, 200, 14);

        jLabel23.setForeground(new java.awt.Color(255, 255, 255));
        jLabel23.setText("TREND_ANALYSIS_INDICATOR");
        jPanel1.add(jLabel23);
        jLabel23.setBounds(440, 220, 200, 14);

        jLabel24.setForeground(new java.awt.Color(255, 255, 255));
        jLabel24.setText("EARNED_VALUE_INDICATOR");
        jPanel1.add(jLabel24);
        jLabel24.setBounds(440, 250, 200, 14);

        jLabel25.setForeground(new java.awt.Color(255, 255, 255));
        jLabel25.setText("PERCENTAGE_OF_COMPLETION");
        jPanel1.add(jLabel25);
        jLabel25.setBounds(440, 280, 200, 14);

        jLabel26.setForeground(new java.awt.Color(255, 255, 255));
        jLabel26.setText("SALES_DOC_DATE_INIDICATOR");
        jPanel1.add(jLabel26);
        jLabel26.setBounds(440, 310, 200, 14);

        jLabel27.setForeground(new java.awt.Color(255, 255, 255));
        jLabel27.setText("INVOICE_PERCENTAGE");
        jPanel1.add(jLabel27);
        jLabel27.setBounds(440, 340, 200, 14);
        jPanel1.add(jTextField15);
        jTextField15.setBounds(650, 40, 100, 20);
        jPanel1.add(jTextField16);
        jTextField16.setBounds(650, 70, 100, 20);
        jPanel1.add(jTextField17);
        jTextField17.setBounds(650, 100, 100, 20);
        jPanel1.add(jTextField18);
        jTextField18.setBounds(650, 130, 100, 20);
        jPanel1.add(jTextField19);
        jTextField19.setBounds(650, 160, 100, 20);
        jPanel1.add(jTextField20);
        jTextField20.setBounds(650, 190, 100, 20);
        jPanel1.add(jTextField22);
        jTextField22.setBounds(650, 250, 100, 20);
        jPanel1.add(jTextField21);
        jTextField21.setBounds(650, 220, 100, 20);
        jPanel1.add(jTextField23);
        jTextField23.setBounds(650, 280, 100, 20);
        jPanel1.add(jTextField24);
        jTextField24.setBounds(650, 310, 100, 20);
        jPanel1.add(jTextField25);
        jTextField25.setBounds(650, 340, 100, 20);

        ok.setText("Ok");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });
        jPanel1.add(ok);
        ok.setBounds(290, 440, 100, 23);

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        jPanel1.add(cancel);
        cancel.setBounds(440, 440, 100, 23);

        add(jPanel1);
        jPanel1.setBounds(10, 60, 810, 490);

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("PSP_ELEMENT");
        add(jLabel3);
        jLabel3.setBounds(230, 10, 140, 14);
        add(jTextField1);
        jTextField1.setBounds(230, 30, 130, 20);

        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("MILESTONE_NUMBER");
        add(jLabel4);
        jLabel4.setBounds(410, 10, 140, 14);
        add(jTextField2);
        jTextField2.setBounds(410, 30, 120, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void runActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runActionPerformed
    

        if(src==0) {
            JOptionPane.showMessageDialog(cm, "You need to select one " +
                    "option", "Error", JOptionPane.ERROR_MESSAGE);
            this.clear();
            this.init();

        } else
            if(src==2) {

                try {
                        Milestone milestone = Stone.get(cm.project, jTextField1.getText(), jTextField2.getText());
                        this.setDialogFromStructure(milestone);
                        jPanel1.setVisible(true);
                        run.setVisible(false);

                } catch (ReturnException ex) {
                    JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
                }

            } else

            if(src==3) {
                try {
                        Stone.delete(jTextField1.getText(), jTextField2.getText());
                        JOptionPane.showMessageDialog(cm, "Milestone Removed", "Info", JOptionPane.INFORMATION_MESSAGE);
                } catch (ReturnException ex) {
                    JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
                }

            }
    }//GEN-LAST:event_runActionPerformed

    private void TiposItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_TiposItemStateChanged

        JComboBox cb = (JComboBox)evt.getSource();

        src = cb.getSelectedIndex();

        switch(src) {
            case 0:
                break;
            case 1:
                this.clear();
                this.init();
                jTextField2.setEnabled(true);
                jLabel4.setEnabled(true);
                jPanel1.setVisible(true);
                break;
            case 2:
                this.clear();
                this.init();
                jTextField2.setEnabled(true);
                jLabel4.setEnabled(true);
                run.setVisible(true);
                break;
            case 3:
                this.clear();
                this.init();
                jTextField2.setEnabled(true);
                jLabel4.setEnabled(true);
                run.setVisible(true);
                break;


        }

    }//GEN-LAST:event_TiposItemStateChanged

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        // TODO add your handling code here:


       if(src==1) {
            try {
                Stone.create(getListFromDialog());
            } catch (ReturnException ex) {
                JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
            }
            this.clear();
            this.init();
        } else
            if(src==2) {
                try {
                    Stone.maintain(getListFromDialog());
                } catch (ReturnException ex) {
                    JOptionPane.showMessageDialog(cm, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
                }
                this.clear();
                this.init();
                this.clear();
                this.init();
            }
            Tipos.setSelectedIndex(0);
    }//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed

        this.clear();
        this.initC();
        Tipos.setSelectedIndex(0);
}//GEN-LAST:event_cancelActionPerformed

    
 
    
    private CManager cm;
    private int src = -1;
    protected String milestone;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox Tipos;
    private javax.swing.JButton cancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField14;
    private javax.swing.JTextField jTextField15;
    private javax.swing.JTextField jTextField16;
    private javax.swing.JTextField jTextField17;
    private javax.swing.JTextField jTextField18;
    private javax.swing.JTextField jTextField19;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField20;
    private javax.swing.JTextField jTextField21;
    private javax.swing.JTextField jTextField22;
    private javax.swing.JTextField jTextField23;
    private javax.swing.JTextField jTextField24;
    private javax.swing.JTextField jTextField25;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JButton ok;
    private javax.swing.JButton run;
    // End of variables declaration//GEN-END:variables
    
}
